package edu.ynu.dao;
// default package

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;

import edu.ynu.po.Magazine;

/**
 	* A data access object (DAO) providing persistence and search support for Magazine entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .Magazine
  * @author MyEclipse Persistence Tools 
 */

public class MagazineDAO extends BaseHibernateDAO  {
		 private static final Log log = LogFactory.getLog(MagazineDAO.class);
		//property constants
	public static final String NAME = "name";
	public static final String TYPE = "type";
	public static final String SERIAL_NO = "serialNo";
	public static final String CYCLE = "cycle";
	public static final String ISSUE_COUNT = "issueCount";
	public static final String UNIT = "unit";
	public static final String PRICE = "price";
	public static final String STOCK = "stock";
	public static final String TARGET_CST = "targetCst";
	public static final String REMARK = "remark";



    
    public void save(Magazine transientInstance) {
        log.debug("saving Magazine instance");
        try {
        	Session session = getSession();
        	session.clear();
        	Transaction trans = session.beginTransaction();
            session.save(transientInstance);
            trans.commit();
            session.close();
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(Magazine persistentInstance) {
        log.debug("deleting Magazine instance");
        try {
        	Session session = getSession();
        	session.clear();
        	Transaction trans = session.beginTransaction();
            session.delete(persistentInstance);
            trans.commit();
            session.close();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
	
	public void update(Magazine transientInstance) {
        log.debug("saving Magazine instance");
        try {
        	Session session = getSession();
        	session.clear();
        	Transaction trans = session.beginTransaction();
            session.update(transientInstance);
            trans.commit();
            session.close();
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
    public Magazine findById( java.lang.Integer id) {
        log.debug("getting Magazine instance with id: " + id);
        try {
            Magazine instance = (Magazine) getSession()
                    .get("edu.ynu.po.Magazine", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(Magazine instance) {
        log.debug("finding Magazine instance by example");
        try {
            List results = getSession()
                    .createCriteria("Magazine")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Magazine instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Magazine as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByName(Object name
	) {
		return findByProperty(NAME, name
		);
	}
	
	public List findByType(Object type
	) {
		return findByProperty(TYPE, type
		);
	}
	
	public List findBySerialNo(Object serialNo
	) {
		return findByProperty(SERIAL_NO, serialNo
		);
	}
	
	public List findByCycle(Object cycle
	) {
		return findByProperty(CYCLE, cycle
		);
	}
	
	public List findByIssueCount(Object issueCount
	) {
		return findByProperty(ISSUE_COUNT, issueCount
		);
	}
	
	public List findByUnit(Object unit
	) {
		return findByProperty(UNIT, unit
		);
	}
	
	public List findByPrice(Object price
	) {
		return findByProperty(PRICE, price
		);
	}
	
	public List findByStock(Object stock
	) {
		return findByProperty(STOCK, stock
		);
	}
	
	public List findByTargetCst(Object targetCst
	) {
		return findByProperty(TARGET_CST, targetCst
		);
	}
	
	public List findByRemark(Object remark
	) {
		return findByProperty(REMARK, remark
		);
	}
	

	public List findAll(Map paramMap) {
		log.debug("finding all Magazine instances");
		try {
			String name = (String)paramMap.get("name");
			String cycle = (String)paramMap.get("cycle");
			String targetCst = (String)paramMap.get("targetCst");
			Criteria c = getSession().createCriteria(Magazine.class);
			if(StringUtils.isNotEmpty(name)){
				c.add(Expression.like("name", "%"+name+"%"));
			}
			if(StringUtils.isNotEmpty(cycle)){
				c.add(Expression.like("cycle", "%"+cycle+"%"));
			}
			if(StringUtils.isNotEmpty(targetCst)){
				c.add(Expression.like("targetCst", "%"+targetCst+"%"));
			}
			return c.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public Magazine merge(Magazine detachedInstance) {
        log.debug("merging Magazine instance");
        try {
            Magazine result = (Magazine) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Magazine instance) {
        log.debug("attaching dirty Magazine instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Magazine instance) {
        log.debug("attaching clean Magazine instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}